package com.ys.service.business.impl;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.entity.business.TpAmazonShopExchangeRate;
import com.ys.enume.sys.RegexUtilEnum;
import com.ys.mapper.business.TpAmazonShopExchangeRateMapper;
import com.ys.service.business.TpAmazonShopExchangeRateService;
import com.ys.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service("TpAmazonShopExchangeRateService")
public class TpAmazonShopExchangeRateImpl implements TpAmazonShopExchangeRateService {

    @Autowired
    private TpAmazonShopExchangeRateMapper tpAmazonShopExchangeRateMapper;

    /**
     * 新增币种
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R add(Map<String, Object> params) throws Exception {
        //验证
        String msg = yzMsg(params,1);
        if (msg != null){
            return R.error(msg);
        }
        TpAmazonShopExchangeRate tpAmazonShopExchangeRate = JSON.parseObject(JSON.toJSONString(params), TpAmazonShopExchangeRate.class);

        tpAmazonShopExchangeRateMapper.insert(tpAmazonShopExchangeRate);
        return R.ok(AddDto.closeAndReload());
    }

    /**
     * 修改币种
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R edit(Map<String, Object> params) throws Exception {

        //验证
        String msg = yzMsg(params,2);
        if (msg != null){
            return R.error(msg);
        }
        TpAmazonShopExchangeRate tpAmazonShopExchangeRate = JSON.parseObject(JSON.toJSONString(params), TpAmazonShopExchangeRate.class);

        tpAmazonShopExchangeRateMapper.update(tpAmazonShopExchangeRate);
        return R.ok(AddDto.closeAndReload());
    }

    private String yzMsg(Map<String, Object> params,int type){
        if (type == 2){
            if (params.get("id") == null){
                return "币种id不允许为空!";
            }
        }

        if (params.get("name") == null){
            return "货币名称不允许为空!";
        }
        if (params.get("name").toString().length() > 50){
            return "货币名称长度不允许超过50个字符!";
        }
        if (params.get("code") == null){
            return "货币代码不允许为空!";
        }
        if (params.get("code").toString().length() > 50){
            return "货币代码长度不允许超过50个字符!";
        }
        if (type == 1){
            if (new TpAmazonShopExchangeRate().setCode(params.get("code").toString()).count() != 0){
                return "货币代码已存在,核实后再操作!";
            }
        }else {
            if (new TpAmazonShopExchangeRate().setCode(params.get("code").toString()).where("A.id !=" + (params.get("id"))).count() != 0){
                return "货币代码已存在,核实后再操作!";
            }
        }
        if (params.get("exchangeRate1") == null){
            return "官方汇率不允许为空!";
        }
        Pattern pattern = Pattern.compile(RegexUtilEnum.DefaultValue.DECIMAL_OR_NOT.value);

        Matcher matcher = pattern.matcher(params.get("exchangeRate1").toString());
        if (matcher.matches() == false){
            return "官方汇率只允许填写小数!";
        }
        if (params.get("exchangeRate2") != null){
            matcher = pattern.matcher(params.get("exchangeRate2").toString());
            if (matcher.matches() == false){
                return "公司汇率只允许填写小数!";
            }
        }
        return null;
    }

}
